<?php


namespace app\mobile\controller;


use think\cache\driver\Redis;
use think\Controller;
use app\index\model\Cosplay;
class Book extends Controller
{
    public function bookInfo()
    {
        $id = input('id');
        $book = db('book')->where('state',1)->find($id);//书籍信息
        $newChapter = db('chapter')->where('book_id',$id)->order('index desc')->find();//最新更新章节
//        $recommend = db("recommend")->where('book_id',$id)->count();//总推荐票数
//        $exceptional = (new \app\index\controller\Book())->exceptional($id);//打赏总数
        $author_books = db('book')->where('m_id',$book['m_id'])->where('state',1)->select();//作者书籍
        $cate_books = db('book')->where('cate_id',$book['cate_id'])->where('state',1)->limit(6)->select();//同类型书籍
        $site_recommend = db("book")->order('id','desc')->where('state',1)->limit(6)->select();//站点推荐
        $this->book_pv($id,$book);//增加书籍点击
        $this->assign([
            'book' => $book,
            'newChapter' => $newChapter,
//            'recommend' => $recommend,
            'author_books' => $author_books,
            'cate_books' => $cate_books,
//            'exceptional' => $exceptional,
            'site_recommend' => $site_recommend
        ]);
        return view('book_info');
    }
    public function chapterList()
    {
        $book_id = input('book_id');
        if($this->request->isAjax()){
            $book_id = input('book_id');
//            $book = db('book')->where('state',1)->find($book_id);
            $chapter_list['free'] = db('chapter')->where('book_id',$book_id)->where('is_free',0)->order('index desc')->select();
            $chapter_list['nofree'] = db('chapter')->where('book_id',$book_id)->where('is_free',1)->order('index desc')->select();
            return $chapter_list;
        }
        $book = db('book')->where('state',1)->find($book_id);
        $chapter_list['free'] = db('chapter')->where('book_id',$book_id)->where('is_free',0)->select();
        $chapter_list['nofree'] = db('chapter')->where('book_id',$book_id)->where('is_free',1)->select();
        $chapter_total = count($chapter_list['free'])+count($chapter_list['nofree']);
        $this->assign([
            'book' => $book,
            'chapter_list' => $chapter_list,
            'chapter_total' => $chapter_total
        ]);
        return view('chapter_list');
    }
    //推荐
    public function tuijian()
    {
        $member_id = session('member_id');
        $book_id = input('book_id');
        if(empty($member_id)) return ['state' => -1,'msg' => '您还没有登录，请先登录。'];
//        $member = db('member')->find($member_id);
        $recommer = db('recommend')->where('member_id',$member_id)->where('book_id',$book_id)->find();
        if(!empty($recommer)) return ['state' => -1,'msg' => '请勿重复投票'];
        $res = db('recommend')->insert(['member_id' => $member_id,'book_id' => $book_id,'num' =>1,'current' => time()]);
        db('book')->where('id',$book_id)->setInc('tuijian');
        if(!$res) return ['state' => -1,'msg' => '系统错误,请刷新后重试'];
        $num = db('book')->where('id',$book_id)->find();
        return ['state' => 200,'msg' => 'SUCCESS','num' => $num['tuijian']];
    }
    //打赏
    public function dasang()
    {
        $member_id = session('member_id');
        $book_id = input('book_id');
        if(empty($member_id)) return ['state' => -1,'msg' => '您还没有登录，请先登录。'];
        $exceptional = db('exceptional')->where('book_id',$book_id)->where('member_id',$member_id)->find();
//        dump($exceptional);die;
        $member = db('member')->find($member_id);
        if($member['money'] < 5) return ['state' => -1,'msg' => '书币不足，请先充值'];
        $book = db('book')->find($book_id);
//        dump($book);die;
        if(!empty($exceptional)){
            //更新数据
            $res = db('exceptional')->where('book_id',$book_id)->where('member_id',$member_id)->setInc('num',1);
        }else{
            //添加数据
            $res = db('exceptional')->insert(['book_id' => $book_id,'member_id' => $member_id,'num' => 1,'current_time' => time()]);
        }
//        dump($res);
        if(!$res) return ['state' => -1,'msg' => '系统错误，请稍后再试'];
        db('book')->where('id',$book_id)->update(['dasang' => $book['dasang']+1]);
        db('member')->where('id',$member_id)->update(['money'=>$member['money']-5]);
        $num = db('book')->where('id',$book_id)->find();
        return ['state' => 200,'msg' => 'SUCCESS','num' => $num['dasang']];
    }

    //更多
    public function moreBook(){
        $data = null;
        $name = null;
        $recommendation = input('recommendation');
        if(!empty($recommendation)){
            $data = db("book")->cache(true,60)->where('recommendation',$recommendation)->order('update','desc')->select();
            $name = "精品推荐";
        }
        $more_list = input('more_list');
        if(!empty($more_list)){
            $data = db("book")->cache(true,60)->where('more_list',$more_list)->order('update','desc')->select();
            $name = "追更榜";
        }
        $original = input('original');
        if(!empty($original)){
            $data = db("book")->cache(true,60)->where('original',$original)->order('update','desc')->select();
            $name = "原创精品";
        }
        $new_book = input('new_book');
        if(!empty($new_book)){
            $data =  db("book")->where('new_book_list',1)->order('update','desc')->cache(true,60)->limit(12)->select();
            $name = "新书榜";
        }
        $original_new = input('original_new');
        if(!empty($original_new)){
            $data = db("book")->cache(true,60)->where('original_new',$original_new)->order('update','desc')->select();
            $name = "原创新书";
        }
        $news = input('news');
        if(!empty($news)){
            $data = db("book")->cache(true,60)->where('new',$news)->order('update','desc')->select();
            $name = "新书入库";
        }
        $ticket_list = input('ticket_list');
        if(!empty($ticket_list)){
            $data = db("book")->cache(true,60)->where('ticket_list',$ticket_list)->order('update','desc')->select();
            $name = "银票榜";
        }
//        dump($data);
        $this->assign([
            'data' => $data,
            'name' => $name
        ]);
        return view("more_book");
    }
//    书库
    public function bookStore()
    {
        if($this->request->post()){
            $data = db('book')->where('state',1)->order('id desc')->paginate(12);
            return $data;
        }
        $first_data = db("cate")->cache(900)->where('pid',0)->select();  //查询出第一梯队类别
        $two_data = db("cate")->cache(900)->where('pid',1)->select();   //查询出第二梯队类别
        $this->assign([
            'member_id' => session('member_id'),
            'first_data' => $first_data,
            'two_data' => $two_data
        ]);
        return view('book_store');
    }
    public function cate()
    {
        $id = input('id');
        $two_data = db("cate")->where('pid',$id)->select();
        return ['data' => $two_data];
    }
    public function postBookStore(){
        $param = $this->request->param();
        $data = null;
        $where = array();
        if($param['cate'] == 0){
            $where[] = ['cate_pid','=',$param['channel']];
        }else{
            $where[] = ['cate_id','=',$param['cate']];
        }
        /*if($param['is_free'] != -1){
            if($param['is_free'] == 1){
                $where[] = ['free_start','>',0];
            }else{
                $where[] = ['free_start','=',$param['is_free']];
            }
        }*/
        if($param['complete'] != -1){
            $where[] = ['complete','=',$param['complete']];
        }
        $data = db('book')->where('state',1)->where($where)->paginate(30);
        return $data;
    }
    //计算书籍点击量
    public function book_pv($book_id,$book)
    {
        $ip = request()->ip();
        $key = $ip . ':' . $book_id;
        $redis = new Redis();
        $val = $redis->get($key);
        if (!$val) {
            $redis->set($key, $book_id, 300);
            db('book')->where('id', $book_id)->update(['book_pv' => $book['book_pv'] + 1]);
        }
    }
    //女频
    public function girl()
    {
        $member_id = session('member_id') ?? null;
        if(!empty($member_id)){
            $member = db('member')->find($member_id);
            $this->assign('member',$member);
            $this->assign('member_id',$member_id);
        }
        $redis = new Redis();
        $data = $redis->get('data');
        if(empty($data)){
            $girl_book = db('book')->where('cate_pid',2)->limit(60)->select();
            $redis->set('data',$girl_book,24*3600);
        }else{
            $girl_book = $data;
        }
        $this->assign('girl_book',$girl_book);
        return view('girl');
    }
    //推荐
    public function recommend(){
        $member_id = session('member_id') ?? null;
        if(!empty($member_id)){
            $member = db('member')->find($member_id);
            $this->assign('member',$member);
            $this->assign('member_id',$member_id);
        }
        $redis = new Redis();
       $cosplay = db('cosplay')->column('book_id');
       $key = implode(',',$cosplay);
       $data = $redis->get($key);
       if(empty($data)){
           $data = db('book')->where('id','in',$cosplay)->select();
           $redis->set($key,$data,24*3600);
       }
        $this->assign('data',$data);
        return view('recommend');
    }
}